package com.woniuxy.mapper.impl;

import com.woniuxy.mapper.StuDao;
import com.woniuxy.entity.Stu;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @author 作者
 * @create 2025-06-17 16:25
 */
public class StuDaoImpl implements StuDao {
    QueryRunner queryRunner = new QueryRunner();

    @Override
    public void insert(Stu stu) throws Exception {
        queryRunner.update(JdbcUtil.getConnection(),
                "insert into stu values(?,?,?)",stu.getSid(),stu.getSname(),stu.getAge());
    }

    @Override
    public Stu selectBySid(int sid) throws Exception {
        Stu stu = queryRunner.query(JdbcUtil.getConnection(),
                "select * from stu where sid=?",
                new BeanHandler<>(Stu.class),
                sid);
        return stu;
    }

    @Override
    public List<Stu> selectAll() throws Exception {
        return queryRunner.query(JdbcUtil.getConnection(),
                "select * from stu ",
                new BeanListHandler<>(Stu.class));
    }
}
